package _1weiyunsuan;

public class 出现k次找出1次 {
    public static void main(String[] args) {
        //构造
        int[] array = new int[]{3, 3, 3, 6, 6, 6, 9, 9, 8, 9, 2, 2, 2, 7, 7, 7};
        int k = 3;

        int len = array.length;
        int maxLen = 0;//最长的三进制数位
        char[][] kRadix = new char[len][];
        for (int i = 0; i < len; i++) {
            //转为3进制
            kRadix[i] = new StringBuilder(Integer.toString(array[i], k)).reverse().toString().toCharArray();
            if (kRadix[i].length > maxLen)
                maxLen = kRadix[i].length;
        }
        int[] resArray = new int[maxLen];
        //不进位加法
        for (int i = 0; i < len; i++) {
            for (int j = 0; j < maxLen; j++) {
                //开始遍历
                if (j >= kRadix[i].length) {
                    resArray[j] += 0;
                } else resArray[j] += (kRadix[i][j] - '0');
            }
        }
        //得到结果数组，再处理
        int res = 0;
        for (int i = 0; i < resArray.length; i++) {
            res += (int) ((resArray[i] % k) * Math.pow(k, i));
        }
        System.out.println("那个没重复的数：" + res);
    }
}
